package com.huluobo.a2206douyindemo.setting;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.base.bean.CommonBeanResult;
import com.huluobo.base.mvvm.BaseViewModel;
import com.huluobo.base.net.Const;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/8/29.
 */
public class SettingViewModel extends BaseViewModel<SettingRepo> {

    MutableLiveData<CommonBeanResult> updateIconSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateIconFailed = new MutableLiveData<>();

    public void updateUserIcon(String filePath) {
        //1.创建文件对象
        File file = new File(filePath);
        //2.封装为body
        RequestBody body = RequestBody.create(MediaType.parse(Const.MEDIA_TYPE), file);
        //3.封装为Part
        //第一个参数,接口文档中part的参数,第二个参数是文件名,从上面创建的文件对象中拿到,第三个是body,具体的文件
        MultipartBody.Part part = MultipartBody.Part.createFormData(Const.PARAM_FILE, file.getName(), body);
        repo.updateUserIcon(part, updateIconSuccess, updateIconFailed);
    }

    @Override
    protected SettingRepo createRepo() {
        return new SettingRepo();
    }
}
